package com.littlebee.storysuper.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.littlebee.storysuper.R;
import com.littlebee.storysuper.entry.User;
import com.littlebee.storysuper.utils.Constant;
import com.littlebee.storysuper.utils.NetWorkUtils;
import com.littlebee.storysuper.utils.Utils;
import com.littlebee.storysuper.widget.CostomCircleCountDown;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * 启动页
 */
public class LoadingActivity extends AppCompatActivity {
    RelativeLayout show_bg;
    CostomCircleCountDown countdownProgressView;
    boolean isFinsh=false;
    TextView btn_submit;
    ImageView iv;
    SharedPreferences sharedPreferences;
    boolean isNewPhoto=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        show_bg=(RelativeLayout)findViewById(R.id.show_bg);
        btn_submit=(TextView)findViewById(R.id.btn_submit);
        iv=(ImageView)findViewById(R.id.iv);
        //Utils.saveUserEntry(null);
        User user=Utils.getUserEntry();
        //LogUtils.i("xhx","XHX:"+user.getMobile());
        if(user==null){
            Intent intent=new Intent(this,RegisterActivity.class);
            intent.putExtra("isFirst",true);
            startActivity(intent);
            finish();
            return;
        }
        sharedPreferences = getSharedPreferences(Constant.appName, Context.MODE_PRIVATE); //私有数据
//        String lauchimg=sharedPreferences.getString("lauch","");
//        if(!TextUtils.isEmpty(lauchimg)) {
//            btn_submit.setText(sharedPreferences.getString("newphotonotifytxt",""));
//            //btn_submit.setBackgroundColor(getColor(R.color.trans));
//            //模拟显示图片
//            Glide.with(LoadingActivity.this)
//                    .load(lauchimg)
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
//                    .placeholder(R.color.module_video_bg).into(iv);
//        }
       // btn_submit.setVisibility(View.GONE);
        countdownProgressView = (CostomCircleCountDown) findViewById(R.id.costomCirc);
        countdownProgressView.setmMaxTime(5);
        countdownProgressView.startAnim();
        countdownProgressView.setOnFinishAnimListener(new CostomCircleCountDown.OnFinishAnimListener() {
            @Override
            public void onFinish() {
                Intent intent=new Intent(LoadingActivity.this,MainActivity.class);
                intent.putExtra("isNewPhoto",isNewPhoto);
                startActivity(intent);
                finish();
            }
        });
        getInfo();
        countdownProgressView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                countdownProgressView.stopAnim();
                Intent intent=new Intent(LoadingActivity.this,MainActivity.class);
                intent.putExtra("isNewPhoto",isNewPhoto);
                startActivity(intent);
                finish();
            }
        });
    }

    /**
     * 获取启动图
     */
    public void getInfo() {
        User user= Utils.getUserEntry();
        if(user!=null) {
            HashMap<String, String> map = new HashMap<>();
            map.put("phoneId", user.getMobile());
//        map.put("password","123456");
            NetWorkUtils.post(LoadingActivity.this, "getnewsplashinfo", map, false,new NetWorkUtils.IListener() {

                //@RequiresApi(api = Build.VERSION_CODES.M)
                @Override
                public void onSuccess(String result, JSONObject resObj) {
                    // TODO: 2017/8/16   下载图片

                    //{"result":{"rc":0,"rd":"getnewsplashinfo ok"},
                    // "response":{"newstphotoid":"1",
                    // "newstphotourl":"http://47.92.37.125:9090/piecefile/1/1-1.jpg",
                    // "newphotonotifytxt":"您的宝宝又有新的照片了，赶紧去宝宝相册查看吧",
                    // "nonewphotonotifytxt":"陪伴是对宝宝最好的爱"}}sp
                    try {
                        String url=resObj.getJSONObject("response").getString("newstphotourl");
                        String newphotonotifytxt=resObj.getJSONObject("response").getString("newphotonotifytxt").replace("，","\n");
                        String nonewphotonotifytxt=resObj.getJSONObject("response").getString("nonewphotonotifytxt");
                        if(sharedPreferences.getString("lauch","").equals(url)){
                            isNewPhoto=false;
                            btn_submit.setVisibility(View.GONE);
                        }else{
                            isNewPhoto=true;
                            btn_submit.setVisibility(View.VISIBLE);

                        }
                        sharedPreferences.edit().putString("lauch",url).commit();
                        sharedPreferences.edit().putString("newphotonotifytxt",newphotonotifytxt).commit();
                        sharedPreferences.edit().putString("nonewphotonotifytxt",nonewphotonotifytxt).commit();

//                        Glide.with(LoadingActivity.this)
//                                .load(url)
//                                .downloadOnly(500, 500);
                        Glide.with(LoadingActivity.this)
                                .load(url)
                                .placeholder(R.color.module_video_bg)
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .into(iv);
                        btn_submit.setText(sharedPreferences.getString("newphotonotifytxt",""));
                        //btn_submit.setBackgroundColor(getColor(R.color.trans));


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onError(String result, String code, String msg) {
//                    sharedPreferences.edit().putString("lauch","").commit();
//                    sharedPreferences.edit().putString("newphotonotifytxt","").commit();
//                    sharedPreferences.edit().putString("nonewphotonotifytxt","").commit();
                }
            });
        }
    }
    @Override
    public void onBackPressed() {
        countdownProgressView.stopAnim();
        super.onBackPressed();
    }
}
